package org.elephas.service;

import org.elephas.model.ElephasConfiguration;
import org.elephas.persistence.Dao;
import org.hibernate.HibernateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("elephasService")
@Transactional
public class ElephasService {

	@Autowired
	private Dao dao;

	/**
	 * Loads the configuration object or creates a new one.
	 * 
	 * @return the configuration
	 */
	public ElephasConfiguration load() {
		ElephasConfiguration config = null;
		try {
			config = (ElephasConfiguration) dao.createCriteria(
					ElephasConfiguration.class).uniqueResult();
		} catch (HibernateException ex) {
			config = new ElephasConfiguration();
		}
		return config;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public ElephasConfiguration save(ElephasConfiguration config) {
		return (ElephasConfiguration) dao.merge(config);
	}
}
